package com.eudi.springmvc.controller.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eudi.springmvc.dto.user.UserDTO;
import com.eudi.springmvc.dto.user.UserQuery;
import com.eudi.springmvc.service.UserService;

@RestController
@RequestMapping(value = "/user")
public class UserController {
	
	private static Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public List<UserDTO> getUsers(@RequestBody UserQuery query) {
		logger.info("获取用户列表，参数为" + query);
		System.out.println(query);
		List<UserDTO> userList = new ArrayList<UserDTO>();
		UserDTO u1 = new UserDTO();
		u1.setId(1);
		u1.setName("zhang");
		userList.add(u1);

		UserDTO u2 = new UserDTO();
		u2.setId(2);
		u2.setName("wang");
		userList.add(u2);

		UserDTO u3 = new UserDTO();
		u3.setId(3);
		u3.setName("li");
		userList.add(u3);

		return userList;
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public UserDTO addUser(@RequestBody UserDTO user) {
		user.setId(1);
		return user;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
	public UserDTO getUser(@PathVariable int id) {
		return userService.getUser(id);
	}
	
	public UserController() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					logger.info(new Date());
				}
				
			}}).start();
	}
}
